package com.digitaleye.dpslite;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.RelativeLayout;
import com.downpayment.dps.R;



public class SplashActivity extends Activity {
	
	private static final int SPLASH_DISPLAY_TIME = 3500; // splash screen delay time
	
	private static final int SPLASH_DELAY_TIME = 1500; //time to wait after splash animation
	
	ImageView redBar;
	ImageView blueBar;
	ImageView yellowBar;
	ImageView greenBar;
	ImageView logo;
	
	DBHelper dbHelper;
	
	WebHelper webHelper;
	
	@Override
	public void onBackPressed(){
	
		finish();
	}
	
	@Override
	public void onConfigurationChanged(Configuration newConfig){
		super.onConfigurationChanged(newConfig);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    
	    setContentView(R.layout.splash_layout);
	    
	    this.dbHelper = new DBHelper(this);
	    
	    this.webHelper = new WebHelper(this);
	    
	    
	    /** always first time when starting up app **/
	    this.dbHelper.setBoolean(dbHelper.COLUMN_FIRST_TIME, 1);
	    
	    redBar = (ImageView) findViewById(R.id.redBar);
	    blueBar = (ImageView) findViewById(R.id.blueBar);
	    yellowBar = (ImageView) findViewById(R.id.yellowBar);
	    greenBar = (ImageView) findViewById(R.id.greenBar);
	    
	    logo = (ImageView) findViewById(R.id.splashHouses);
	    

	    
	   

	    
	    adjustBarSize();
	    
	    animateRedBar();
	    
	    
	    new Handler().postDelayed(new Runnable() {
	        public void run() {

	            animateBlueBar();

	        }
	    }, SPLASH_DISPLAY_TIME/4-500);
	    
	    new Handler().postDelayed(new Runnable() {
	        public void run() {

	        	animateYellowBar();

	        }
	    }, SPLASH_DISPLAY_TIME/2-500);
	    
	    
	    
	    new Handler().postDelayed(new Runnable() {
	        public void run() {

	        	animateGreenBar();

	        }
	    }, SPLASH_DISPLAY_TIME*3/4-500);
	    
	    
	}

	
	/** 
	 * adjusts bar sizes and location based on screen width
	 * 
	 * uses magic numbers and mystery formulas... sorry!
	 */
	public void adjustBarSize(){
		
		/** get the screen size **/
		DisplayMetrics displaymetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
		double height = displaymetrics.heightPixels;
		double width = displaymetrics.widthPixels;
		
		int intervals = 9;
		double intervalWidth = (width/intervals);
		
		int redInterval = 1;
		double redBarWidth = width*1/15;
		int redBarHeight = 100;//redBar.getHeight();
		int redBarX = (int) (redInterval*intervalWidth*1.2);
		
		RelativeLayout.LayoutParams redParams = (RelativeLayout.LayoutParams) redBar.getLayoutParams();//new RelativeLayout.LayoutParams((int) redBarWidth, redBarHeight);
		redParams.leftMargin = redBarX;
		redParams.bottomMargin = (int) -10;
		redParams.height = redBarHeight;
		redParams.width = (int) redBarWidth;
		
		redBar.setLayoutParams(redParams);
		
		//redParams.addRule(RelativeLayout.CENTER_VERTICAL, R.id.relativeLayout);
		
		
		int blueInterval = 1;
		double blueBarWidth = width*1/12;
		int blueBarHeight = 150;//blueBar.getHeight();
		int blueBarX = (int) (blueInterval*intervalWidth*.50);
		
		RelativeLayout.LayoutParams blueParams = (RelativeLayout.LayoutParams) blueBar.getLayoutParams();//new RelativeLayout.LayoutParams((int) blueBarWidth, blueBarHeight);
		blueParams.leftMargin = blueBarX;
		blueParams.height = blueBarHeight;
		blueParams.width = (int) blueBarWidth;
		blueParams.addRule(RelativeLayout.CENTER_VERTICAL, R.id.relativeLayout);
		
		
		
		
		int yellowInterval = 1;
		double yellowBarWidth = width*1/12;
		int yellowBarHeight = 210;//yellowBar.getHeight();
		int yellowBarX = (int) (yellowInterval*intervalWidth*.75);
		
		RelativeLayout.LayoutParams yellowParams = (RelativeLayout.LayoutParams) yellowBar.getLayoutParams();//new RelativeLayout.LayoutParams((int) yellowBarWidth, yellowBarHeight);
		yellowParams.leftMargin = yellowBarX;
		yellowParams.height = yellowBarHeight;
		yellowParams.width = (int) yellowBarWidth;
		yellowParams.addRule(RelativeLayout.CENTER_VERTICAL, R.id.relativeLayout);
		
		
		int greenInterval = 3;
		double greenBarWidth = width*1/10;
		int greenBarHeight = 275;//greenBar.getHeight();
		int greenBarX = (int) (greenInterval*intervalWidth*.40);
		
		RelativeLayout.LayoutParams greenParams = (RelativeLayout.LayoutParams) greenBar.getLayoutParams();//new RelativeLayout.LayoutParams((int) greenBarWidth, greenBarHeight);
		greenParams.leftMargin = greenBarX;
		greenParams.height = greenBarHeight;
		greenParams.width = (int) greenBarWidth;
		greenParams.addRule(RelativeLayout.CENTER_VERTICAL, R.id.relativeLayout);
		
		
	}
	
	public void animateRedBar(){
		
		Animation animation = AnimationUtils.loadAnimation(this, R.anim.grow);
		
		animation.setDuration(this.SPLASH_DISPLAY_TIME/4);
		
		animation.setAnimationListener(new AnimationListener(){

			@Override
			public void onAnimationEnd(Animation arg0) {

				
			}

			@Override
			public void onAnimationRepeat(Animation arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onAnimationStart(Animation arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
		
		redBar.clearAnimation();
		redBar.startAnimation(animation);
		
		this.webHelper.incrementUsage();

		
	}
	
public void animateBlueBar(){
		
		blueBar.setVisibility(View.VISIBLE);
		
		Animation animation = AnimationUtils.loadAnimation(this, R.anim.grow);
		
		animation.setDuration(this.SPLASH_DISPLAY_TIME/4);
		
		animation.setAnimationListener(new AnimationListener(){

			@Override
			public void onAnimationEnd(Animation arg0) {
				
			}

			@Override
			public void onAnimationRepeat(Animation arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onAnimationStart(Animation arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
		
		blueBar.clearAnimation();
		blueBar.startAnimation(animation);

		
	}


	public void animateYellowBar(){
		
		yellowBar.setVisibility(View.VISIBLE);
		
		Animation animation = AnimationUtils.loadAnimation(this, R.anim.grow_yellow);
		
		animation.setDuration(this.SPLASH_DISPLAY_TIME/4);
		
		animation.setAnimationListener(new AnimationListener(){

			@Override
			public void onAnimationEnd(Animation arg0) {
				// TODO Auto-generated method stub
				//animateGreenBar();			
			}

			@Override
			public void onAnimationRepeat(Animation arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onAnimationStart(Animation arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
		
		yellowBar.clearAnimation();
		yellowBar.startAnimation(animation);
	
	}
	
	public void animateGreenBar(){
		
		greenBar.setVisibility(View.VISIBLE);
		
		Animation animation = AnimationUtils.loadAnimation(this, R.anim.grow);
		
		animation.setDuration(this.SPLASH_DISPLAY_TIME/4);
		
		animation.setAnimationListener(new AnimationListener(){
	
			@Override
			public void onAnimationEnd(Animation arg0) {
				// TODO Auto-generated method stub
				
				
				final Handler mHandler = new Handler();

				Runnable pauseAfterAnimation = new Runnable() {
				    public void run() {
				        // Do some stuff that you want to do here
				    	
			            Intent intent = new Intent();
			            intent.setClass(SplashActivity.this, MainActivity.class);

			            SplashActivity.this.finish();
			            SplashActivity.this.startActivity(intent);
			            
			            
			            
			            // transition from splash to main menu
			            overridePendingTransition(R.anim.activityfadein,
			                    R.anim.splashfadout);
			            

			            // You could do this call if you wanted it to be periodic:
				        //mHandler.postDelayed(this, 5000 );

				    }
				};
				
				mHandler.postDelayed(pauseAfterAnimation, SPLASH_DELAY_TIME);
				
				    
				    
				



			}
	
			@Override
			public void onAnimationRepeat(Animation arg0) {
				// TODO Auto-generated method stub
				
			}
	
			@Override
			public void onAnimationStart(Animation arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
		
		
		greenBar.clearAnimation();
		greenBar.startAnimation(animation);
	
	}
	
	public void startService(){
		
		Intent serviceIntent = new Intent(this, AddMoneyService.class);
        startService(serviceIntent);
		
	}
	
}
